#!/usr/bin/python
# -*-coding:utf-8-*-
"""公共方法"""
import re
import datetime
import time
import json
import os
import logging
import pandas as pd
# import logutils


def HtmlImgP(content):
    """
    过滤html 只保留 img/p标签
    :param content:
    :return: str
    """
    p = re.compile('(?!<(img|p).*?>)<.*?>')
    content = p.sub("", content)
    fh_str = "\n|\t|  "
    fh_arr = fh_str.split('|')
    for fh in fh_arr:
        content = content.replace(fh, "")
    return content


def TodayTime():
    """
    返回当天日期 格式 2022-07-19
    :return: 2022-07-19
    """
    return str(datetime.date.today())


def saveLog(content):
    """
    记录错误日志
    :param content: 日志内容
    :return:
    """
    if not os.path.exists("../log"):
        os.mkdir("../log")
    file_name = TodayTime() + " " + str(time.time())[:10]
    with open("../log/" + file_name + '.log', 'w', encoding='utf-8') as f:
        f.write(content + ' 路径：' + os.getcwd())


def saveData(data, file_name="data_1"):
    """
    保存数据到文件中
    :param data:
    :param file_name: 文件名称
    :return:
    """
    if not os.path.exists(file_name + '.json'):
        with open(file_name + '.json', 'w', encoding='utf-8') as f:
            f.write('')
    json.dump(data, open(file_name + '.json', 'a', encoding='utf-8'), indent=2, ensure_ascii=False)
    with open(file_name + '.json', 'a', encoding='utf-8') as f:
        f.write('\n')


def list_split(items, n):
    """
    将数组 list1 每 n 个切分一次
    :param items:
    :param n:
    :return:
    """
    return [items[i:i + n] for i in range(0, len(items), n)]


def data_time(cols):
    """
    2022年07月27日 15:25:00 日期格式化为 2022-07-27 15:25:00
    :param cols:
    :return:
    """
    cols = cols.replace('年', '.')
    cols = cols.replace('月', '.')
    cols = cols.replace('日', '')
    cols = pd.to_datetime(cols)
    print(cols)
    return cols


def is_contain_chinese(check_str):
    """
    判断字符串中是否包含中文
    :param check_str: {str} 需要检测的字符串
    :return: {bool} 包含返回True， 不包含返回False
    """
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False


def get_items(req):
    """使用Xpath解析页面，提取商品信息"""
    # if (type(req) == str):
    #     html = etree.HTML(req)
    # else:
    #     html = etree.HTML(req.text)

    with open('/cloud/cloud_disk/users/huh/nlp/smart_home/script/super_Reptile/kk.txt', 'w') as fp:
        fp.writelines(str(req.text))